package com.logindemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.logindemo.domain.Account;
import com.logindemo.domain.LoginAccount;
import com.logindemo.repository.AccountRepository;
import com.logindemo.service.AccountService;
import com.logindemo.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountRepository accountRepository;
    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public HashMap<String, String> login(Account account) {
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(account.getUsername(), account.getPassword());
        Authentication authenticate;
        try {
             authenticate = authenticationManager.authenticate(authenticationToken);
        }catch (BadCredentialsException e){
            throw new RuntimeException("密码错误");
        }catch (AuthenticationException e){
            throw new RuntimeException("账号不存在");
        }
        LoginAccount loginAccount = (LoginAccount)authenticate.getPrincipal();
        String jwt = JwtUtil.createJwt(loginAccount.getAccount().getId());
        redisTemplate.opsForValue().set(loginAccount.getAccount().getId(), loginAccount);
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", jwt);
        return hashMap;
    }

    @Override
    public Boolean logout() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginAccount loginAccount = (LoginAccount)authentication.getPrincipal();
        Boolean delete = redisTemplate.delete(loginAccount.getAccount().getId());
        return delete;
    }

    @Override
    public Account register(Account account) {
        //加密密码
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(account.getPassword());
        account.setPassword(encode);
        int insert = accountRepository.insert(account);
        LambdaQueryWrapper<Account> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Account::getUsername, account.getUsername());
        return accountRepository.selectOne(lambdaQueryWrapper);
    }
}
